package clientApp.gui;

import javax.swing.JDialog;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import clientApp.netlogic.MyMessageListener;
import clientApp.netlogic.SocketMessageManager;

public class LogInDialog extends JDialog {
	private JTextField loginTextField;
	private JPasswordField passField;
	private JLabel loginLabel;
	private JLabel passLabel;
	private boolean statusLoging;		//определяет состояние входа в систему true == иницализация проведена успешно false == вход запрещен
	private JButton okButton;
	private JButton cancelButton;
	private SocketMessageManager myManager;
	
	public LogInDialog(JFrame ownerFrame, String name, boolean modal, SocketMessageManager manager) {
		super(ownerFrame, name, modal);
		
		getContentPane().setLayout(null);
		
		myManager = manager;
		
		loginTextField = new JTextField();
		loginTextField.setToolTipText("Введiть iм'я користувача");
		loginTextField.setBounds(97, 22, 146, 26);
		getContentPane().add(loginTextField);
		loginTextField.setColumns(10);
		
		loginLabel = new JLabel("Login");
		loginLabel.setFont(new Font("Dialog", Font.BOLD, 12));
		loginLabel.setBounds(12, 27, 70, 15);
		getContentPane().add(loginLabel);
		
		passLabel = new JLabel("Password");
		passLabel.setBounds(12, 67, 70, 15);
		getContentPane().add(passLabel);
		
		passField = new JPasswordField();
		passField.setToolTipText("Введiть пароль");
		passField.setBounds(97, 60, 146, 29);
		getContentPane().add(passField);
		
		ButtonHandler buttonHandler = new ButtonHandler(); 
		
		okButton = new JButton("Ok");
		okButton.setIcon(new ImageIcon(LogInDialog.class.getResource("/resources/icons/tick.png")));
		okButton.setBounds(12, 101, 117, 25);
		getContentPane().add(okButton);
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (!loginTextField.getText().equals("") && (!passField.getPassword().equals(""))){
					myManager.runClient(loginTextField.getText(), new String(passField.getPassword()));
					statusLoging = myManager.isConnected();
					LogInDialog.this.dispose();
				}
				else {
					JOptionPane.showMessageDialog(null, "Введiть данi");
				}
			}
		});
		
		cancelButton = new JButton("Вiдмiнити");
		cancelButton.setIcon(new ImageIcon(LogInDialog.class.getResource("/resources/icons/cross.png")));
		cancelButton.setBounds(141, 101, 129, 25);
		cancelButton.addActionListener(buttonHandler);
		getContentPane().add(cancelButton);
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setSize(285, 174);
	}

	public boolean isStatusLoging() {
		return statusLoging;
	}

	public void setStatusLoging(boolean statusLoging) {
		this.statusLoging = statusLoging;
	}
		
	
	public JButton getOkButton() {
		return okButton;
	}






	//класс обработки событий кнопок
	private class ButtonHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent evt) {
			// TODO Auto-generated method stub
			if (evt.getSource() == cancelButton){
				closeDialog();
			}
		}
		
		private void closeDialog(){
			loginTextField = null;
			passField = null;
			LogInDialog.this.dispose();
		}
		
	}
}
